package com.eurasia.assignment.infrastructure.entity;

import com.eurasia.assignment.domain.TeamMember;
import com.eurasia.assignment.domain.TeamRole;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;

/**
 * 团队成员中间表实体
 */
@Entity
@Table(name = "team_members")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class TeamMemberEntity {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    @Column(name = "user_id", nullable = false)
    private Long userId;
    
    @Enumerated(EnumType.STRING)
    @Column(name = "team_role", nullable = false)
    private TeamRole teamRole;
    
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "assignment_id", nullable = false)
    @ToString.Exclude
    @EqualsAndHashCode.Exclude
    private ProjectAssignmentEntity assignment;
    
    /**
     * 从领域模型创建实体
     */
    public static TeamMemberEntity fromDomain(TeamMember domain) {
        TeamMemberEntity entity = new TeamMemberEntity();
        entity.setId(domain.getId());
        entity.setUserId(domain.getUserId());
        entity.setTeamRole(domain.getTeamRole());
        return entity;
    }
    
    /**
     * 转换为领域模型
     */
    public TeamMember toDomain() {
        TeamMember domain = new TeamMember();
        domain.setId(this.id);
        domain.setUserId(this.userId);
        domain.setTeamRole(this.teamRole);
        return domain;
    }
} 